package com.wwp.tbshop.view.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.SearchActivity;
import com.wwp.tbshop.view.adapter.CategoryPagerAdapter;
import com.wwp.tbshop.view.fragment.category.CategoryBaseFragment;
import com.wwp.tbshop.view.fragment.category.ChildDressFragment;
import com.wwp.tbshop.view.fragment.category.ChildToyFragment;
import com.wwp.tbshop.view.fragment.category.ChildUsesFragment;
import com.wwp.tbshop.view.fragment.category.DailySupplyFragment;
import com.wwp.tbshop.view.fragment.category.FashionFragment;
import com.wwp.tbshop.view.fragment.category.MomFragment;
import com.wwp.tbshop.view.fragment.category.OtherFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 楠GG on 2017/6/15.
 */

public class CategoryFragment extends Fragment {
    private static final String TAG = "CategoryFragment";
    @Bind(R.id.search)
    ImageView mSearch;
    @Bind(R.id.tab_layout)
    TabLayout mTabLayout;
    @Bind(R.id.view_pager)
    ViewPager mViewPager;
    private CategoryPagerAdapter mCategoryPagerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_category, null);
        ButterKnife.bind(this, rootView);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initFragment();

        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mCategoryPagerAdapter = new CategoryPagerAdapter(getChildFragmentManager(), getActivity(), this);
        mCategoryPagerAdapter.setFragmentList(mFragmentList);
        mViewPager.setAdapter(mCategoryPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        for(int i = 0; i < 15; i++){
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(mCategoryPagerAdapter.getTabView(i));
        }

        mViewPager.addOnPageChangeListener(mOnPageChangeListener);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mCategoryFragmentPresenter.loadCategoryInfo();

    }


    public ViewPager.OnPageChangeListener mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
//            final CategoryBaseFragment fragment = (CategoryBaseFragment) mCategoryPagerAdapter.getItem(position);
            CategoryBaseFragment fragment = (CategoryBaseFragment) mFragmentList.get(position);
            fragment.loadData();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    private List<Fragment> mFragmentList = new ArrayList<>();

    private void initFragment() {
        if(mFragmentList.size()>0)return;

        mFragmentList.add(new MomFragment());
        mFragmentList.add(new FashionFragment());
        mFragmentList.add(new ChildUsesFragment());
        mFragmentList.add(new DailySupplyFragment());
        mFragmentList.add(new ChildDressFragment());
        mFragmentList.add(new ChildToyFragment());

        for (int i = 0; i < 9; i++) {
            mFragmentList.add(new OtherFragment());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search:
                //搜索
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                getActivity().startActivity(intent);
                break;
        }
    }
}
